<?php
/**
 * 账单
 */
namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class DicsValidation extends BaseValidation
{
	public function initialize()
	{
		$this->add(
			"id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"name",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  // "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"pid",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  // "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"cate_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_hot",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"order_by",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_multi",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_display",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_del",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
	}

	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	parent::beforeValidation($data, $entity, $messages);
    	
    	$data->pid = property_exists($data,'pid') ? $data->pid : 0;
    	$data->cate_id = property_exists($data,'cate_id') ? $data->cate_id : 0;
    	$data->order_by = property_exists($data,'order_by') ? $data->order_by : 100;
    	$data->is_hot = property_exists($data,'is_hot') ? $data->is_hot : 0;
    	$data->is_multi = property_exists($data,'is_multi') ? $data->is_multi : 0;
    	$data->is_display = property_exists($data,'is_display') ? $data->is_display : 1;
    	$data->is_del = property_exists($data,'is_del') ? $data->is_del : 0;

        return true;
    }
}